﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SwParaPlatform2.dlgUIConfig
{
    public class UIConfigDelegate: IDelegate
    {
        private readonly IView view;
        private readonly IModel model;


        public UIConfigDelegate(IView view, IModel model)
        {
            //定义信号
            this.view = view;
            this.model = model;

            this.view.SaveOrder += OnSaveOrder;
            this.view.DelEditRow += OnDelEditRow;
            this.view.AddEditRow += OnAddEditRow;
            this.view.ChangeEditRow += OnChangeEditRow;
            this.view.InputTypeChanged += OnInputTypeChanged;
            this.view.SelectRow += OnSelectRow;
            this.view.OnCheckedChanged += OnSelectMainViewPic;
            view.NextClicked += OnNextClicked;
            view.LoadDataBtnClicked += OnLoadDataBtnClicked;
            view.SaveBtnClicked += OnSaveBtnClicked;
            view.UpBtnClicked += OnUpBtnClicked;
            view.DownBtnClicked += OnDownBtnClicked;

            this.model.GridDataChanged += OnGridDataChanged;
            this.model.SelectParaChanged += OnSelectParaChanged;
            this.model.DoSelectIndex += OnSelectIndex;
            this.model.SelectViewFilePathChanged += OnSelectViewFilePathChanged;

            //初始化界面
            InitializeInputTypeList();
        }

        public void DisConnect()
        {
            this.view.DelEditRow -= OnDelEditRow;
            this.view.AddEditRow -= OnAddEditRow;
            this.view.ChangeEditRow -= OnChangeEditRow;
            this.view.InputTypeChanged -= OnInputTypeChanged;
            this.view.SelectRow -= OnSelectRow;
            this.view.OnCheckedChanged -= OnSelectMainViewPic;
            view.NextClicked -= OnNextClicked;
            view.LoadDataBtnClicked += OnLoadDataBtnClicked;
            view.SaveBtnClicked -= OnSaveBtnClicked;
            view.UpBtnClicked -= OnUpBtnClicked;
            view.DownBtnClicked -= OnDownBtnClicked;

            this.model.GridDataChanged -= OnGridDataChanged;
            this.model.SelectParaChanged -= OnSelectParaChanged;
            this.model.DoSelectIndex -= OnSelectIndex;
        }

        #region 回调
        private void OnDelEditRow(object sender, int Tempint)
        {
            try
            {
                model.DelEditRow();
            }
            catch (Exception ex)
            {
                view.ShowError(ex.Message);
            }
            finally
            {
            }
        }
        
        private void OnSaveOrder(object sender, GenericEventArgs<List<string>> lOrder)
        {
            try
            {
                model.SaveOrder(lOrder.Data);
            }
            catch (Exception ex)
            {
                view.ShowError(ex.Message);
            }
            finally
            {
            }
        }

        private void OnAddEditRow(object sender, GenericEventArgs<Dictionary<string, object>> dRowDict)
        {
            try
            {
                model.AddEditRow(dRowDict.Data);
            }
            catch (Exception ex)
            {
                view.ShowError(ex.Message);
            }
            finally
            {
            }
        }

        private void OnChangeEditRow(object sender, GenericEventArgs<(int, Dictionary<string, object>)> e)
        {
        }

        public void OnInputTypeChanged(string selectedType)
        {
            view.OnSelectEditType(selectedType);

        }
        
        public void OnSelectRow(object sender, string sSelectPara)
        {
            model.OnSelectRow(sSelectPara);

        }

        public void OnSelectMainViewPic(object sender, string viewFilePath)
        {
            model.OnSelectMainViewPic(viewFilePath);
        }

        public void OnNextClicked(object sender, EventArgs e)
        {
            model.SaveData();
        }
        
        public void OnLoadDataBtnClicked(object sender, EventArgs e)
        {
            //model.LoadData();
            model.LoadAllData();
        }
        
        public void OnSaveBtnClicked(object sender, EventArgs e)
        {
            model.SaveData();
        }
        
        public void OnUpBtnClicked(object sender, EventArgs e)
        {
            model.UpBtn_Click();
        }
        
        public void OnDownBtnClicked(object sender, EventArgs e)
        {
            model.DownBtn_Click();
        }
        
        

        #endregion

        public void InitializeInputTypeList()
        {
            var inputTypeList = model.GetInputTypeList();
            view.SetInputTypeList(inputTypeList);
        }

        private void OnGridDataChanged()
        {
            var dGridData = model.GetGridDataDict();
            
            List<string> argList = model.GetOrder();

            // 创建一个新的字典来存储排序后的结果
            Dictionary<string, Dictionary<string, object>> sortedDict = new Dictionary<string, Dictionary<string, object>>();

            // 遍历排序列表，将对应的元素按顺序加入到新字典中
            int iIndex = 0;
            foreach (var key in argList)
            {
                if (dGridData.ContainsKey(key))
                {
                    sortedDict.Add(key, dGridData[key]);
                    dGridData[key]["序号"] = iIndex;
                    iIndex++;
                }
            }

            var sortedData = dGridData.OrderBy(kv => Convert.ToInt32(kv.Value["序号"])).ToList();
            view.RefreshDataGrid(sortedData);
        }
        
        private void OnSelectParaChanged()
        {
            var sSelectPara = model.GetSelectPara();
            var dGridData = model.GetGridDataDict();
            var dSelectData = dGridData[sSelectPara];
            view.OnSelectPara(dSelectData);
        }
        
        private void OnSelectIndex(object sender, int iIndex)
        {
            view.OnSelectIndex(iIndex);
        }
        
        private void OnSelectViewFilePathChanged()
        {
            string sPath = model.GetViewFilePath();
            view.RefreshSelectPic(sPath);
        }


    }

}
